import 'package:dio/dio.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get_it/get_it.dart';
import 'package:sonora/bloc/blockList/sj_block_list_bloc.dart';
import 'package:sonora/bloc/followList/sj_follow_list_bloc.dart';
import 'package:sonora/bloc/home/sj_home_bloc.dart';
import 'package:sonora/bloc/medal/sj_medal_bloc.dart';
import 'package:sonora/bloc/mine/sj_mine_bloc.dart';
import 'package:sonora/bloc/userInfo/sj_user_info_bloc.dart';
import 'package:sonora/bloc/video/sj_video_bloc.dart';
import 'package:sonora/network/sj_network.dart';
import 'package:sonora/repository/sj_home_repository.dart';
import 'package:sonora/repository/sj_medal_repository.dart';
import 'package:sonora/repository/sj_mine_repository.dart';
import 'package:sonora/repository/sj_video_repository.dart';
import 'package:sonora/services/sj_api_client.dart';
import 'package:sonora/services/sj_api_home_service.dart';
import 'package:sonora/services/sj_api_medal_service.dart';
import 'package:sonora/services/sj_api_mine_service.dart';
import 'package:sonora/services/sj_api_translate_client.dart';
import 'package:sonora/services/sj_api_video_service.dart';
import 'package:sonora/utils/sj_app_tracking_transparency.dart';

final getit = GetIt.instance;
final EventBus eventBus = EventBus();

void appInitial() {
  initPlugin();
  configLoading();

  getit.registerLazySingleton<Dio>(() => SjNetwork.getDefaultDio());
  getit.registerLazySingleton<SjApiClient>(() => SjApiClient(getit<Dio>()));
  getit.registerLazySingleton<SjApiHomeService>(
      () => SjApiHomeService(getit<SjApiClient>()));
  getit.registerLazySingleton<SjHomeRepository>(
      () => SjHomeRepository(getit<SjApiHomeService>()));
  getit.registerLazySingleton<SjHomeBloc>(
      () => SjHomeBloc(getit<SjHomeRepository>()));

  getit.registerLazySingleton<SjUserInfoBloc>(() => SjUserInfoBloc());
  getit.registerLazySingleton<SjFollowListBloc>(() => SjFollowListBloc());
  getit.registerFactory<SjBlockListBloc>(() => SjBlockListBloc());

  getit.registerLazySingleton<SjApiTranslateClient>(() => SjApiTranslateClient(
      Dio()
        ..interceptors.add(LogInterceptor(
            request: true,
            requestHeader: true,
            responseHeader: true,
            responseBody: true,
            error: true))));
  getit.registerLazySingleton<SjApiVideoService>(() =>
      SjApiVideoService(getit<SjApiClient>(), getit<SjApiTranslateClient>()));
  getit.registerLazySingleton<SjVideoRepository>(
      () => SjVideoRepository(getit<SjApiVideoService>()));
  getit.registerFactory<SjVideoBloc>(
      () => SjVideoBloc(getit<SjVideoRepository>()));

  getit.registerLazySingleton<SjApiMineService>(
      () => SjApiMineService(getit<SjApiClient>()));
  getit.registerLazySingleton<SjMineRepository>(
      () => SjMineRepository(getit<SjApiMineService>()));
  getit.registerLazySingleton<SjMineBloc>(
      () => SjMineBloc(getit<SjMineRepository>()));

  getit.registerLazySingleton<SjApiMedalService>(
      () => SjApiMedalService(getit<SjApiClient>()));
  getit.registerLazySingleton<SjMedalRepository>(
      () => SjMedalRepository(getit<SjApiMedalService>()));
  getit.registerFactory<SjMedalBloc>(
      () => SjMedalBloc(getit<SjMedalRepository>()));
}

void configLoading() {
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)
    ..indicatorType = EasyLoadingIndicatorType.fadingCircle
    ..loadingStyle = EasyLoadingStyle.dark
    ..indicatorSize = 45.0
    ..radius = 10.0
    ..progressColor = Colors.yellow
    ..backgroundColor = Colors.green
    ..indicatorColor = Colors.yellow
    ..textColor = Colors.yellow
    ..maskColor = Colors.blue.withOpacity(0.5)
    ..userInteractions = false
    ..dismissOnTap = false;
  // ..customAnimation = CustomAnimation();
}

void initPlugin() {
  SjAppTrackingTransparency().init();
}
